package com.grm.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grm.common.entity.Result;
import com.grm.common.annotation.HasPerm;
import com.grm.common.annotation.Log;
import com.grm.sys.model.SysDictType;
import com.grm.sys.request.dict.type.SysDictTypeAddParam;
import com.grm.sys.request.dict.type.SysDictTypeQueryParam;
import com.grm.sys.request.dict.type.SysDictTypeUpdateParam;
import com.grm.sys.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/dict/type")
public class SysDictTypeController {
    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PostMapping("/list")
    public Result querySelective(
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(required = false, defaultValue = "30") Integer pageSize,
        @RequestBody SysDictTypeQueryParam queryParam) {
        // 开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<SysDictType> sysDictTypes = sysDictTypeService.querySelective(queryParam);
        PageInfo<SysDictType> pageInfo = new PageInfo<>(sysDictTypes);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }

    @Log("修改字典类型")
    @PutMapping("/update")
    @HasPerm("sys:dict:type:update")
    public Result update(@RequestBody SysDictTypeUpdateParam updateParam) {
        sysDictTypeService.update(updateParam);
        return Result.success();
    }

    /**
     * 添加字典类型
     */
    @Log("添加字典类型")
    @HasPerm("sys:dict:type:add")
    @PostMapping("/add")
    public Result add(@RequestBody SysDictTypeAddParam addParam) {
        sysDictTypeService.add(addParam);
        return Result.success();
    }

    /**
     * 删除字典类型
     */
    @Log("删除字典类型")
    @PostMapping("/delete")
    @HasPerm("sys:dict:type:delete")
    public Result delete(@RequestBody List<Long> ids) {
        sysDictTypeService.delete(ids);
        return Result.success();
    }
}
